//
//  T3FAnnotation_Pin.swift
//  t3_map
//
//  Created by zhu on 2024/11/21.
//

import UIKit
import t3_assets

/// 内置大头针的常规样式
class T3FPinAnnotationView: UIView, T3FAnnotationView {
    
    var annotationDidClicked: (([String : Any]?) -> Void)?
    
    struct Options {
        let imageOption: T3FImageOption
        let textOption: T3FTextOption?

        init?(json: [String : Any]) {

            guard let image = json["image"] as? [String: Any] else { return nil }
            guard let imageOption = T3FImageOption(json: image) else { return nil }

            self.imageOption = imageOption

            if let text = json["text"] as? [String: Any] {

                if let textOption = T3FTextOption(json: text) {
                    self.textOption = textOption
                }
                else {
                    self.textOption = nil
                }
            }
            else {
                self.textOption = nil
            }
        }
    }

    let options: Options

    init(options: Options) {
        self.options = options
        super.init(frame: .zero)

        addSubview(imageView)
        imageView.image = UIImage.t3_assetsImage(name: options.imageOption.imageName)
        imageView.snp.remakeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-3)
            make.width.equalTo(options.imageOption.width)
            make.height.equalTo(options.imageOption.height)
        }

        if let textOption = options.textOption {
            addSubview(addressTextLabel)

            addressTextLabel.text = textOption.text
            addressTextLabel.textColor = textOption.normalStyle.uiColor
            addressTextLabel.font = textOption.normalStyle.uifont
            addressTextLabel.snp.remakeConstraints { make in
                make.left.equalTo(imageView.snp.right)
                make.top.equalTo(imageView.snp.bottom).offset(-3)
                make.width.lessThanOrEqualTo(108)
            }
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func update(argvs: [String : Any]?) {

    }

    lazy var addressTextLabel: UILabel = {
        var label = UILabel()
        label.numberOfLines = 2
        return label
    }()

    lazy var imageView: UIImageView = UIImageView()
}

